package com.reebake.ideal.security.web;

import com.reebake.ideal.servlet.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import java.io.IOException;

public class ResourceAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
	
	public ResourceAuthenticationEntryPoint(String loginFormUrl) {
		super(loginFormUrl);
	}

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {
		if(WebUtil.isAjaxRequest(request)
				|| WebUtil.isEventStreamRequest(request)
				|| HttpMethod.OPTIONS.matches(request.getMethod())) {
			response.setStatus(HttpStatus.UNAUTHORIZED.value());
		}else {
			super.commence(request, response, authException);
		}
	}
}
